<template>
  <div class="container">
    <van-nav-bar
      class="app-nav-bar"
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
    />
    <van-form input-align="right" @submit="onSubmit">
      <van-cell title="单元格" value="内容">
        <van-image
          class="avatar"
          :src="userInfo.avatarUrl"
        />
        <div slot="title">头像</div>
      </van-cell>
      <van-field
        v-model="userInfo.name"
        name="姓名"
        label="姓名"
        placeholder="姓名"
        :rules="[{ required: true, message: '请填写姓名' }]"
      />
      <van-field
        v-model="userInfo.username"
        name="账号"
        label="账号"
        placeholder="账号"
        readonly
        :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
        readonly
        clickable
        name="picker"
        :value="userInfo.sex"
        label="性别"
        placeholder=""
        @click="showPicker = true"
      />
      <van-popup v-model="showPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="columns"
          @confirm="onConfirm"
          @cancel="showPicker = false"
        />
      </van-popup>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          type="info"
          block
        >提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { update } from '@/api/user'
export default {
  name: '',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {},
      columns: ['男', '女'],
      showPicker: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {
    this.userInfo = this.$store.state.user
  },
  methods: {
    onConfirm (value) {
      this.userInfo.sex = value
      this.showPicker = false
    },
    onSubmit () {
      console.log('点击按钮', this.userInfo)
      update(this.userInfo).then(res => {
        console.log('update', res)
        this.$toast.success('修改成功，请重新登录')
        setTimeout(() => {
          localStorage.clear()
          window.location.reload()
        }, 500)
        // this.$store.state.user = res.data.data
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.avatar {
  width: 66px;
}
.van-cell__title, .van-cell__value {
  margin: auto 0;
}
.login-btn-wrap {
  padding: 26px 16px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
    .van-button__text {
      font-size: 15px;
    }
  }
}
</style>
